<?
class Sieu_thi_dia_oc_model extends Model{
	
	var $_datagrid_name = 'dt1';
	var $_message;
	var $_createddate1;
	var $_tab_id = 0;
	//thong tin dia oc
	var $_id;	
	var $_createddate;
	var $_firstaccepteddate;
	var $_posteddate;
	var $_accepteddate;
	var $_entrystatus;
	
	var $_categoryid;
	var $_typeid;
	var $_directionid;
	var $_districtid;
	var $_entrytypeid;
	var $_legalstatusid;
	var $_provinceid;
	var $_unitid;
	var $_unitpriceid;
	
	var $_title;
	var $_description;
	var $_location_number;
	var $_location_street;
	var $_location_ward;
	var $_price;
	var $_is_not_intermediary;
	
	var $_number_of_floor;
	var $_number_of_room;
	var $_number_of_bathroom;
	var $_area_campus_x;
	var $_area_campus_y;
	var $_area_construction_x;
	var $_area_construction_y;
	var $_area_total_used;
	var $_area_alley_width;
	
	var $_has_electric_meter;
	var $_has_water_meter;
	var $_has_homephone;
	var $_has_airconditioner;
	var $_has_waterheater;
	var $_has_car_parking;
	var $_has_garden;
	var $_has_swimmingpool;
	var $_has_near_market;
	var $_has_near_school;
	var $_has_near_hospital;
	var $_has_near_park;
	var $_has_high_intellectual;
	var $_has_bussiness_convenience;
	
	var $_contact_name;
	var $_contact_phone;
	var $_contact_cellphone;
	var $_contact_address;
	var $_contact_note;
	var $_image;
	
	var $_member_name;
	var $_member_address;
	var $_member_email;
	var $status = 0;
	//
	function Sieu_thi_dia_oc_model(){
		parent::Model();
		$this->load->model('catalogs/realestatecategory_model');
		$this->load->model('catalogs/realestatetype_model');
		$this->load->model('catalogs/entrytype_model');
		$this->load->model('catalogs/province_model');		
		$this->load->model('catalogs/unitprice_model');
		$this->load->model('catalogs/unit_model');
		$this->load->model('catalogs/direction_model');
		$this->load->model('catalogs/legalstatus_model');		
		
	}
	
	/*Add by PhucDuong*/
	function tin_khong_su_dung(){
		return $this->_get_thong_tin(5);
	}
	
	function tin_cho_duyet(){
		return $this->_get_thong_tin(1);
	}
	
	function tin_chua_hop_le(){
		return $this->_get_thong_tin(2);
	}
	
	function tin_dang_hien_thi(){
		return $this->_get_thong_tin(3);
	}
	
	function tin_ngung_dang(){
		return $this->_get_thong_tin(4);
	}
	//thong ke
	function thong_ke()
	{
		$this->_tab_id = 1;
		$thongtin_arr = array();
		$this->db->select('realestate.id,customer.createddate,entrystatus');
		$this->db->from('realestate');
		$this->db->join('customer', 'realestate.customerid = customer.id');
		$query = $this->db->get();
		$thongtin = $query->result();
		//
		$thongtin_arr['tin_dang_hien_thi'] = $this->_count_status($thongtin,3);
		$thongtin_arr['tin_cho_duyet'] = $this->_count_status($thongtin,1);
		$thongtin_arr['tin_chua_hop_le'] = $this->_count_status($thongtin,2);
		$thongtin_arr['tin_khong_su_dung'] = $this->_count_status($thongtin,5);
		$thongtin_arr['tin_ngung_dang'] = $this->_count_status($thongtin,4);
		return $thongtin_arr;
	}
	
	function _get_thong_tin($status=''){
		//
		$page = $this->input->post($this->_datagrid_name.'_hidden_page')?$this->input->post($this->_datagrid_name.'_hidden_page'):1;    	
    	$limit = $this->config->item('record_limit');   	
		//
		$this->db->select('realestate.id,title,fullname,realestate.createddate');
		$this->db->from('realestate');
		$this->db->join('customer', 'realestate.customerid = customer.id');
		$this->db->order_by('realestate.createddate','DESC');
		$this->db->where(array('entrystatus' => $status));
		$query = $this->db->get();
		//
    	$count = $query->num_rows;
    	$pages = intval($count/$limit) + ($count%$limit?1:0);
    	$page = $page>$pages?1:$page;
    	$this->db->limit($limit,($page-1)*$limit);
    	$this->db->select('realestate.id,title,fullname,realestate.createddate');
		$this->db->from('realestate');
		$this->db->join('customer', 'realestate.customerid = customer.id');
		$this->db->order_by('realestate.createddate','DESC');
		$this->db->where(array('entrystatus' => $status));
		$query = $this->db->get();
    	$datagrid->id = $this->_datagrid_name;
    	$datagrid->page = $page;
    	$datagrid->pages = $pages;
    	$datagrid->datasource = $query->result_rowlist();
    	return $datagrid;
		//		
	}
	
	function _count_status($info='',$status=''){
		$count = 0;
		foreach($info as $row) {
		if($row->entrystatus == $status)
		$count ++;	
		}
		return $count;
	}
	// tin chi tiet
	function thong_tin(){
		
		$info['id'] 						= $this->_id;		
		$info['createddate'] 				= $this->_createddate;		
		$info['posteddate'] 				= $this->_posteddate;
		$info['accepteddate'] 				= $this->_accepteddate;
		$info['firstaccepteddate'] 			= $this->_firstaccepteddate;
		$info['entrystatus'] 				= $this->_entrystatus;
		
		$info['categoryid'] 				= !empty($this->_categoryid)?$this->_categoryid:$this->realestatecategory_model->first_category();
		$info['typeid'] 					= $this->_typeid;
		$info['directionid'] 				= $this->_directionid;
		$info['districtid'] 				= $this->_districtid;
		$info['entrytypeid'] 				= $this->_entrytypeid;
		$info['legalstatusid'] 				= $this->_legalstatusid;
		$info['provinceid'] 				= $this->_provinceid;
		$info['unitid'] 					= $this->_unitid;
		$info['unitpriceid'] 				= $this->_unitpriceid;
		
		$info['title'] 						= $this->_title;
		$info['description'] 				= $this->_description;
		$info['location_number'] 			= $this->_location_number;
		$info['location_street'] 			= $this->_location_street;
		$info['location_ward'] 				= $this->_location_ward;
		$info['price'] 						= $this->_price;
		$info['is_not_intermediary'] 		= $this->_is_not_intermediary;
		
		$info['number_of_floor'] 			= $this->_number_of_floor;
		$info['number_of_room'] 			= $this->_number_of_room;
		$info['number_of_bathroom'] 		= $this->_number_of_bathroom;
		$info['area_campus_x'] 				= $this->_area_campus_x;
		$info['area_campus_y'] 				= $this->_area_campus_y;
		$info['area_construction_x'] 		= $this->_area_construction_x;
		$info['area_construction_y'] 		= $this->_area_construction_y;
		$info['area_total_used'] 			= $this->_area_total_used;
		$info['area_alley_width'] 			= $this->_area_alley_width;
		
		$info['has_electric_meter'] 		= $this->_has_electric_meter;
		$info['has_water_meter'] 			= $this->_has_water_meter;
		$info['has_homephone'] 				= $this->_has_homephone;
		$info['has_airconditioner'] 		= $this->_has_airconditioner;
		$info['has_waterheater'] 			= $this->_has_waterheater;
		$info['has_car_parking'] 			= $this->_has_car_parking;
		$info['has_garden'] 				= $this->_has_garden;
		$info['has_swimmingpool']			= $this->_has_swimmingpool;
		$info['has_near_market'] 			= $this->_has_near_market;
		$info['has_near_school'] 			= $this->_has_near_school;
		$info['has_near_hospital'] 			= $this->_has_near_hospital;
		$info['has_near_park'] 				= $this->_has_near_park;
		$info['has_high_intellectual'] 		= $this->_has_high_intellectual;
		$info['has_bussiness_convenience']	= $this->_has_bussiness_convenience;
		
		$info['contact_name'] 				= $this->_contact_name;
		$info['contact_phone'] 				= $this->_contact_phone;
		$info['contact_cellphone'] 			= $this->_contact_cellphone;
		$info['contact_address'] 			= $this->_contact_address;
		$info['contact_note'] 				= $this->_contact_note;
		$info['image'] 						= $this->_image;	
			
		#####################################
		$info['category_list'] 				= $this->realestatecategory_model->get_content();		
		$info['type_list'] 					= $this->realestatetype_model->get_content_by_id($info['categoryid']);		
		$info['entrytype_list']				= $this->entrytype_model->get_content();	
		$info['province_list'] 				= $this->province_model->get_content();		
		$info['district_list']				= $this->province_model->get_district_content($info['provinceid']);
		$info['unitprice_list']				= $this->unitprice_model->get_content();
		$info['unit_list']					= $this->unit_model->get_content();	
		$info['direction_list']				= $this->direction_model->get_content();	
		$info['legalstatus_list']			= $this->legalstatus_model->get_content();
		#####################################
		$info['message'] 					= $this->_message;	
		#####################################
		//***********************************************//
		return $info;
	}
	// get thong tin
	function thong_tin_tai_san($id){
		//$this->load->model('thanh_vien/dia_oc_model');			
		$this->_id = $id;
		$query = $this->db->get_where('realestate',array('id'=>$this->_id));
		if(!$query->num_rows()) redirect('thanh_vien','location');
		$r = $query->row();
		
		$this->_firstaccepteddate 			= $r->firstaccepteddate; 
		$this->_posteddate 					= $r->posteddate; 	
		$this->_entrystatus 				= $r->entrystatus; 
		$this->_categoryid 					= $r->categoryid;  
		$this->_typeid 						= $r->typeid; 
		$this->_directionid 				= $r->directionid; 
		$this->_districtid 					=  $r->districtid;  
		$this->_entrytypeid 				= $r->entrytypeid; 
		$this->_legalstatusid 				= $r->legalstatusid;
		$this->_provinceid 					= $r->provinceid; 
		$this->_unitid 						= $r->unitid; 
		$this->_unitpriceid 				= $r->unitpriceid; 
		
		$this->_title 						= $r->title; 
		$this->_description 				= $r->description; 
		$this->_location_number 			= $r->location_number; 
		$this->_location_street 			= $r->location_street; 
		$this->_location_ward 				= $r->location_ward; 
		$this->_price 						= $r->price; 
		$this->_is_not_intermediary 		= $r->is_not_intermediary; 
		
		$this->_number_of_floor 			= $r->number_of_floor; 
		$this->_number_of_room 				= $r->number_of_room; 
		$this->_number_of_bathroom 			= $r->number_of_bathroom; 
		$this->_area_campus_x 				= $r->area_campus_x; 
		$this->_area_campus_y 				= $r->area_campus_y; 
		$this->_area_construction_x 		= $r->area_construction_x; 
		$this->_area_construction_y 		= $r->area_construction_y; 
		$this->_area_total_used 			= $r->area_total_used; 
		$this->_area_alley_width 			= $r->area_alley_width; 
		
		$this->_has_electric_meter 			= $r->has_electric_meter; 
		$this->_has_water_meter 			= $r->has_water_meter; 
		$this->_has_homephone 				= $r->has_homephone; 
		$this->_has_airconditioner 			= $r->has_airconditioner; 
		$this->_has_waterheater 			= $r->has_waterheater; 
		$this->_has_car_parking 			= $r->has_car_parking; 
		$this->_has_garden 					= $r->has_garden; 
		$this->_has_swimmingpool 			= $r->has_swimmingpool; 
		$this->_has_near_market 			= $r->has_near_market; 
		$this->_has_near_school 			= $r->has_near_school; 
		$this->_has_near_hospital 			= $r->has_near_hospital; 
		$this->_has_near_park 				= $r->has_near_park; 
		$this->_has_high_intellectual 		= $r->has_high_intellectual; 
		$this->_has_bussiness_convenience	= $r->has_bussiness_convenience; 
		
		$this->_contact_name 				= $r->contact_name; 
		$this->_contact_phone 				= $r->contact_phone; 
		$this->_contact_cellphone 			= $r->contact_cellphone; 
		$this->_contact_address 			= $r->contact_address; 
		$this->_contact_note 				= $r->contact_note; 
		
		$this->_image                       = $r->image1;
		// thong tin thanh vien 
		$query_mem = $this->db->get_where('customer',array('id'=>$r->customerid));
		if(!$query_mem->num_rows()) redirect('home','location');
		$r_mem = $query_mem->row();
		$this->_member_name                 = $r_mem->fullname;
		$this->_member_address              = $r_mem->address;
		$this->_member_email                = $r_mem->email;
		//		
		return $this->thong_tin();
	}
	
	function luu_tai_san(){
		if(FALSE!==$this->input->post('hidden_status')){			
			
			$this->_id 							= $this->input->post('hidden_value');			
			$this->_entrystatus 				= $this->input->post('hidden_status'); 
			$this->_categoryid 					= $this->input->post('hidden_category'); 
			$this->_typeid 						= $this->input->post('cbo_realestatetype'); 
			$this->_directionid 				= $this->input->post('cbo_direction')!=''?$this->input->post('cbo_direction'):0; 
			$this->_districtid 					= $this->input->post('cbo_districts')!=''?$this->input->post('cbo_districts'):0;  
			$this->_entrytypeid 				= FALSE!==$this->input->post('rdo_entry_type')?$this->input->post('rdo_entry_type'):0; 
			$this->_legalstatusid 				= $this->input->post('cbo_legalstatus')!=''?$this->input->post('cbo_legalstatus'):0; 
			$this->_provinceid 					= $this->input->post('cbo_provinces')!=''?$this->input->post('cbo_provinces'):0; 
			$this->_unitid 						= $this->input->post('cbo_unit')!=''?$this->input->post('cbo_unit'):0; 
			$this->_unitpriceid 				= $this->input->post('cbo_unitprice')!=''?$this->input->post('cbo_unitprice'):0; 
			
			$this->_title 						= $this->input->post('txt_title'); 
			$this->_description 				= $this->input->post('txt_description'); 
			$this->_location_number 			= $this->input->post('txt_location_number'); 
			$this->_location_street 			= $this->input->post('txt_location_street'); 
			$this->_location_ward 				= $this->input->post('txt_location_ward'); 
			$this->_price 						= $this->input->post('txt_price'); 
			$this->_is_not_intermediary 		= FALSE!==$this->input->post('chk_is_not_intermediary')?1:0; 
			
			$this->_number_of_floor 			= $this->input->post('txt_number_of_floor'); 
			$this->_number_of_room 				= $this->input->post('txt_number_of_room'); 
			$this->_number_of_bathroom 			= $this->input->post('txt_number_of_bathroom'); 
			$this->_area_campus_x 				= $this->input->post('txt_area_campus_x'); 
			$this->_area_campus_y 				= $this->input->post('txt_area_campus_y'); 
			$this->_area_construction_x 		= $this->input->post('txt_area_construction_x'); 
			$this->_area_construction_y 		= $this->input->post('txt_area_construction_y'); 
			$this->_area_total_used 			= $this->input->post('txt_area_total_used'); 
			$this->_area_alley_width 			= $this->input->post('txt_area_alley_width'); 
			
			$this->_has_electric_meter 			= FALSE!==$this->input->post('chk_has_electric_meter')?1:0; 
			$this->_has_water_meter 			= FALSE!==$this->input->post('chk_has_water_meter')?1:0; 
			$this->_has_homephone 				= FALSE!==$this->input->post('chk_has_homephone')?1:0; 
			$this->_has_airconditioner 			= FALSE!==$this->input->post('chk_has_airconditioner')?1:0; 
			$this->_has_waterheater 			= FALSE!==$this->input->post('chk_has_waterheater')?1:0; 
			$this->_has_car_parking 			= FALSE!==$this->input->post('chk_has_car_parking')?1:0; 
			$this->_has_garden 					= FALSE!==$this->input->post('chk_has_garden')?1:0; 
			$this->_has_swimmingpool 			= FALSE!==$this->input->post('chk_has_swimmingpool')?1:0; 
			$this->_has_near_market 			= FALSE!==$this->input->post('chk_has_near_market')?1:0; 
			$this->_has_near_school 			= FALSE!==$this->input->post('chk_has_near_school')?1:0; 
			$this->_has_near_hospital 			= FALSE!==$this->input->post('chk_has_near_hospital')?1:0; 
			$this->_has_near_park 				= FALSE!==$this->input->post('chk_has_near_park')?1:0; 
			$this->_has_high_intellectual 		= FALSE!==$this->input->post('chk_has_high_intellectual')?1:0; 
			$this->_has_bussiness_convenience	= FALSE!==$this->input->post('chk_has_bussiness_convenience')?1:0; 
			
			$this->_contact_name 				= $this->input->post('txt_contact_name'); 
			$this->_contact_phone 				= $this->input->post('txt_contact_phone'); 
			$this->_contact_cellphone 			= $this->input->post('txt_contact_cellphone'); 
			$this->_contact_address 			= $this->input->post('txt_contact_address'); 
			$this->_contact_note 				= $this->input->post('txt_contact_note'); 
			
			#####################################
			$this->db->set('categoryid', $this->_categoryid);
			$this->db->set('typeid', $this->_typeid);
			$this->db->set('directionid', $this->_directionid);
			$this->db->set('districtid', $this->_districtid);
			$this->db->set('entrytypeid', $this->_entrytypeid);
			$this->db->set('legalstatusid', $this->_legalstatusid);
			$this->db->set('provinceid', $this->_provinceid);
			$this->db->set('unitid', $this->_unitid);
			$this->db->set('unitpriceid', $this->_unitpriceid);
			
			$this->db->set('title', $this->_title);
			$this->db->set('description', $this->_description);
			$this->db->set('location_number', $this->_location_number);
			$this->db->set('location_street', $this->_location_street);
			$this->db->set('location_ward', $this->_location_ward);
			$this->db->set('price', $this->_price);			
			$this->db->set('is_not_intermediary', $this->_is_not_intermediary);
			
			$this->db->set('number_of_floor', $this->_number_of_floor);
			$this->db->set('number_of_room', $this->_number_of_room);
			$this->db->set('number_of_bathroom', $this->_number_of_bathroom);
			$this->db->set('area_campus_x', $this->_area_campus_x);
			$this->db->set('area_campus_y', $this->_area_campus_y);
			$this->db->set('area_construction_x', $this->_area_construction_x);
			$this->db->set('area_construction_y', $this->_area_construction_y);
			$this->db->set('area_total_used', $this->_area_total_used);
			$this->db->set('area_alley_width', $this->_area_alley_width);
			
			$this->db->set('has_electric_meter', $this->_has_electric_meter);
			$this->db->set('has_water_meter', $this->_has_water_meter);
			$this->db->set('has_homephone', $this->_has_homephone);
			$this->db->set('has_airconditioner', $this->_has_airconditioner);
			$this->db->set('has_waterheater', $this->_has_waterheater);
			$this->db->set('has_car_parking', $this->_has_car_parking);
			$this->db->set('has_garden', $this->_has_garden);
			$this->db->set('has_swimmingpool', $this->_has_swimmingpool);
			$this->db->set('has_near_market', $this->_has_near_market);
			$this->db->set('has_near_school', $this->_has_near_school);
			$this->db->set('has_near_hospital', $this->_has_near_hospital);
			$this->db->set('has_near_park', $this->_has_near_park);
			$this->db->set('has_high_intellectual', $this->_has_high_intellectual);
			$this->db->set('has_bussiness_convenience', $this->_has_bussiness_convenience);
			
			$this->db->set('contact_name', $this->_contact_name);
			$this->db->set('contact_phone', $this->_contact_phone);
			$this->db->set('contact_cellphone', $this->_contact_cellphone);
			$this->db->set('contact_address', $this->_contact_address);
			$this->db->set('contact_note', $this->_contact_note);
			
			// Validate update	
			$status = empty($this->_id)?0:$this->_get_old_status($this->_id);
			/*if($status==3){
				$this->_message = 'Không thể cập nhật tin đang hiển thị';
				return false;
			}*/
			
			// Upload image
			$upload_success = false;
    		$config['upload_path'] = $this->config->item('real_img_path');
			$config['allowed_types'] = $this->config->item('upload_allowed_types');
			$this->load->library('upload', $config);
    		if($this->upload->do_upload('txt_image')){
    			$upload_data = $this->upload->data();
				$this->db->set('image1', $upload_data['file_name']);
				$upload_success = true;
    		}
			
			// Save process
			if(!empty($this->_id)){	// update
				if($upload_success) $this->_deleteimage($this->_id);
				$this->db->where('id',$this->_id);
				$this->db->update('realestate');				
			}
			else{ // insert
				$this->db->insert('realestate');
				$this->_id = $this->db->insert_id();
			}
			
			// Post entry
			if($this->_entrystatus == 3 || $this->_entrystatus == 2 ){				
				$status = $this->_doi_tinh_trang_tin($this->_id, $this->_entrystatus);
			}
			
			$this->status = $status;			
			return true;
		}
		return false;
	}
	function doi_tinh_trang_tin(){
		$status = '';
		var_dump($_REQUEST);
		if(FALSE!==$this->input->post('hidden_status')){
			$id = $this->input->post('hidden_id');			
			switch($this->input->post('hidden_value')){
				case 'post': $status = 1; break;
				case 'stop': $status = 4; break;
				case 'delete': $status = 5; break;
				default: return $status;
			}
			$status = $this->_doi_tinh_trang_tin($id, $status);
		}
		return $status;
	}
	
	function _doi_tinh_trang_tin($id, $status){		
		$auto_accept = $this->config->item('allow_auto_accept_realestate');
		$allow_delete = $this->config->item('allow_delete_realestate');
		// get old value
		$res = $this->db->get_where('realestate',array('id'=>$id));
		$old_status = $res->row()->entrystatus;
		$accepteddate = $res->row()->accepteddate;
		$now = date('Y-m-d H:i:s');		
		// post
		if($status==1){
			$this->db->set('posteddate', $now);
			if($auto_accept){
				$status = 3;
				$this->db->set('accepteddate', $now);
				if(empty($accepteddate)){
					$this->db->set('firstaccepteddate', $now);
				}
			}
		}
		// stop
		if($status==4){ /* do nothing */ }
		// delete
		if($status==5){			
			if($allow_delete || $old_status==0 || $old_status==1 || $old_status==2){
				$this->_deleteimage($id);
				$this->db->delete('realestate', array('id'=>$id));
				return $old_status;
			}			
		}
		// update status
		if($status == 3)
		{
			$this->db->set('accepteddate', $now);
		}
		$this->db->set('entrystatus',$status);
		$this->db->where('id',$id);
		$this->db->update('realestate');
		return $status!=5?$status:$old_status;
	}
	function _get_old_status($id){
		$res = $this->db->get_where('realestate',array('id'=>$id));
		return $res->row()->entrystatus;
	}
	/* Delete old realestate image  */
	function _deleteimage($id){
    	$res = $this->db->get_where('realestate',array('id'=>$id));
    	$image = $res->row()->image1;
    	if(!empty($image) && file_exists($this->config->item('real_img_path').$image)){
    		@unlink($this->config->item('real_img_path').$image);
    	}
    }
	// delete 
	function delete(){
    	if($this->input->post($this->_datagrid_name.'_hidden_value')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_value');
    		$query = $this->db->get_where('realestate',array('id'=>$id));
    		if($query->num_rows()){    					
    			//$this->db->delete('realestate',array('id'=>$id));
				$this->db->set('entrystatus',4);
				$this->db->where('id',$id);
				$this->db->update('realestate');
				return true;
    		}
    	}
    	else if($this->input->post($this->_datagrid_name.'_chk_select')){
    		$id = $this->input->post($this->_datagrid_name.'_chk_select');
    		for($i=0;$i<count($id);$i++){
	    		$query = $this->db->get_where('realestate',array('id'=>$id[$i]));
	    		if($query->num_rows()){    					
	    			//$this->db->delete('realestate',array('id'=>$id[$i]));
					$this->db->set('entrystatus',4);
					$this->db->where('id',$id[$i]);
					$this->db->update('realestate');
	    		}
    		}
			return true;
    	}
    }
	// save trang thai tin
	function save()
	{
		if(FALSE!==$this->input->post('hidden_id')){
    		$id = $this->input->post('hidden_id');
    		$entrystatus = $this->input->post('entrystatus');
    		$this->db->where('id',$id);
    		$this->db->update('realestate',array('entrystatus'=>$entrystatus,'accepteddate'=>date('Y-m-d H:i:s',time('now'))));
    	}
	}
	
}
/* End of file */